<?php
// 本文档自动生成，仅供测试运行
class AdminAction extends Action
{
    
    /**
    +----------------------------------------------------------
    * 默认后台地址
    +----------------------------------------------------------
    */
	function _initialize(){
		if( !isset($_SESSION['adm_name']) or !isset($_SESSION['isAdmin']) ) $this->redirect('Index/login');
	}

      /**
    +----------------------------------------------------------
    * 管理员列表
    +----------------------------------------------------------
    */
	function admins(){
		$data = M('admin')->select();
		$this->assign('data', $data);
		
		$this->assign('pagetitle', '管理员管理');
		$this->display();
	}

	function editadmin(){
	    $id = $_GET['id'];
		if($id == ''){
			$this->assign('pagetitle', '添加管理员');
		}else{
            if($id == 1){if($_SESSION['adm_name'] != 'admin')$this->error('您没有修改权限');}
			$f = M('admin')->where('id='.$id)->find();
			if($f){
				$this->assign($f);	
                $this->assign('pagetitle', '编辑管理员');
			}else{
				$this->error('此用户不存在');
			}
		}
		$this->display();
	}
	
	function doeditadmin(){
	
	    if(!M()->autoCheckToken($_POST)) $this->error('非法提交');
		unset($_POST['__hash__']);
		
		if($_POST['pass'] != '' ){
			if( strlen($_POST['pass']) < 6) $this->error('密码过短！');
			if($_POST['pass'] != $_POST['pass2']) $this->error('您两次输入的密码不一致');
			$map['passwd'] = md5( $_POST['pass'] );
		}
		
		if($_POST['id']){
		     $map['id'] = $_POST['id'];
			 if($_POST['id'] != 1){
			 	$map['adm_name'] = $_POST['adm_name'];
			 }
             if(M('admin')->save($map)){
		     $this->assign('jumpUrl',U('Admin/admins'));
		     $this->success('修改成功！');
	         }else $this->error('和原始密码一样！');
		}else{
		     $map['id'] = $_POST['id'];
             $map['adm_name'] = $_POST['adm_name'];
             if(M('admin')->add($map)){
		     $this->assign('jumpUrl',U('Admin/admins'));
		     $this->success('添加成功！');
	         }else $this->error('添加失败！');
		}
	
	}
	
	function delAdmin(){
	   
		$_POST['ids']	= explode(',', t($_POST['ids']));
		if (empty($_POST['ids'])) {
			echo 0;
			return ;
		}
		$map['id']	= array('in', $_POST['ids']);
		echo M('admin')->where($map)->delete()?'1':'0';
	}
	

}
?>